﻿//-----------------------------------------------------------------------
// <copyright file="WizardMainMenuControl.cs" company="Microsoft Corporation">
//     Copyright (c) Microsoft Corporation 2011. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Internal.TeamFoundationServer.Submit
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Windows.Forms;
    using Properties;

    /// <summary>
    /// SelectionWizard class.
    /// </summary>
    internal partial class WizardMainMenuControl : WizardControlTemplate
    {
        /// <summary>
        /// Controls dictionary, to manage control stack.
        /// </summary>
        private readonly Dictionary<string, WizardControlTemplate> controls = new Dictionary<string, WizardControlTemplate>();

        /// <summary>
        /// Initializes a new instance of the <see cref="WizardMainMenuControl"/> class.
        /// </summary>
        public WizardMainMenuControl()
        {
            this.InitializeComponent();

            this.rdbCreateShelve.Enabled = this.rdbUpdateShelve.Enabled = this.rdbDeleteShelve.Enabled = this.rdbUnShelve.Enabled = TfsSubmitConfig.ShelveEnabled;

            this.rdbValidateChanges.Enabled = TfsSubmitConfig.ValidateChangesEnabled;
            this.rdbCodereview.Enabled = TfsSubmitConfig.CodeReviewEnabled;
            
            this.rdbCheckin.Enabled = TfsSubmitConfig.SubmitChangesEnabled;

            if (this.rdbCreateShelve.Enabled)
            {
                this.rdbCreateShelve.Checked = true;
            }
        }

        /// <summary>
        /// MainMenuAction enumerator.
        /// </summary>
        public enum MainMenuAction
        {
            /// <summary>
            /// Create Shelve option.
            /// </summary>
            CreateShelve = 1,

            /// <summary>
            /// Update Shelve option.
            /// </summary>
            UpdateShelve = 2,

            /// <summary>
            /// Delete Shelve option.
            /// </summary>
            DeleteShelve = 3,

            /// <summary>
            /// Extract Shelve option.
            /// </summary>
            ExtractShelve = 4,

            /// <summary>
            /// Validate Changes option.
            /// </summary>
            ValidateChanges = 5,

            /// <summary>
            /// Start CodeReview option.
            /// </summary>
            StartCodeReview = 6,

            /// <summary>
            /// Check-in Changes option.
            /// </summary>
            CheckinChanges = 7,
        }

        /// <summary>
        /// Update window.
        /// </summary>
        /// <param name="title">Window Title.</param>
        /// <param name="description">Window Description.</param>
        /// <param name="backButtonEnabled">Back Button Enabled?</param>
        /// <param name="nextButtonEnabled">Next Button Enabled?</param>
        /// <param name="nextButtonCaption">Next Button Caption.</param>
        /// <param name="action">Action to perform on control.</param>
        public override void UpdateWindow(Label title, RichTextBox description, out bool backButtonEnabled, out bool nextButtonEnabled, out string nextButtonCaption, object action)
        {
            backButtonEnabled = false;
            nextButtonEnabled = true;
            nextButtonCaption = Resources.DefaultButtonNextCaption;

            title.Text = Resources.WizardMainMenuTitle;
            description.Text = Resources.WizardMainMenuDescription;

            if (action != null)
            {
                MainMenuAction mainMenuAction;
                if (Enum.TryParse(action.ToString(), out mainMenuAction))
                {
                    switch (mainMenuAction)
                    {
                        case MainMenuAction.CreateShelve:
                            this.rdbCreateShelve.Checked = true;
                            break;
                        case MainMenuAction.DeleteShelve:
                            this.rdbDeleteShelve.Checked = true;
                            break;
                        case MainMenuAction.UpdateShelve:
                            this.rdbUpdateShelve.Checked = true;
                            break;
                        case MainMenuAction.ExtractShelve:
                            this.rdbUnShelve.Checked = true;
                            break;
                        case MainMenuAction.ValidateChanges:
                            if (TfsSubmitConfig.ValidateChangesEnabled)
                            {
                                this.rdbValidateChanges.Checked = true;
                            }

                            break;
                        case MainMenuAction.StartCodeReview:
                            if (TfsSubmitConfig.CodeReviewEnabled)
                            {
                                this.rdbCodereview.Checked = true;
                            }

                            break;
                        case MainMenuAction.CheckinChanges:
                            this.rdbCheckin.Checked = true;
                            break;
                        default:
                            Debug.Assert(true, "BUGBUG Action for MainMenu is not valid.");
                            break;
                    }
                }
            }
        }

        /// <summary>
        /// Button next event.
        /// </summary>
        public override void NextStep()
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;

                if (this.rdbCreateShelve.Checked)
                {
                    this.FireOpenControl(LoadControl<ShelveCreate>());
                }
                else if (this.rdbUpdateShelve.Checked)
                {
                    this.FireOpenControl(LoadControl<ShelveUpdate>());
                }
                else if (this.rdbDeleteShelve.Checked)
                {
                    this.FireOpenControl(LoadControl<ShelveDelete>());
                }
                else if (this.rdbUnShelve.Checked)
                {
                    this.FireOpenControl(LoadControl<ShelveUnshelve>());
                }
                else if (this.rdbValidateChanges.Checked)
                {
                    this.FireOpenControl(LoadControl<ValidateChanges>());
                }
                else if (this.rdbCodereview.Checked)
                {
                    this.FireOpenControl(LoadControl<CodeReview>());
                }
                else if (this.rdbCheckin.Checked)
                {
                    this.FireOpenControl(LoadControl<WorkItemPicker>());
                }
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        /// <summary>
        /// Load Control.
        /// </summary>
        /// <typeparam name="T">Control Type to load.</typeparam>
        /// <returns>Return control loaded.</returns>
        private T LoadControl<T>() where T : WizardControlTemplate, new()
        {
            var typeName = typeof(T).Name;

            Debug.Assert(!this.controls.ContainsKey(typeName) || this.controls[typeName].IsDisposed, "BUGBUG control stack BUG.");

            if (this.controls.ContainsKey(typeName) && this.controls[typeName].IsDisposed)
            {
                this.controls.Remove(typeName);
            }

            var newControl = new T();
            this.controls.Add(typeName, newControl);

            return newControl;
        }

        /// <summary>
        /// Operation changed.
        /// </summary>
        /// <param name="sender">Object sender.</param>
        /// <param name="e">Event arguments.</param>
        private void RdbCheckedChanged(object sender, EventArgs e)
        {
            var radioButton = sender as RadioButton;

            if (radioButton != null && radioButton.Checked)
            {
                this.menuDescription.Text = radioButton.Tag as string;
            }
        }
    }
}
